package com.googlecode.icaltohtml.icalparser;

import java_cup.runtime.*;
 

%%

%public
%class Lexer

%unicode

%cupsym ParserSym
%cup
%line
%column

%xstate VAL, UNSUPP
 


 
%{
  	public int x=0;	
	public final boolean debugMode=false;
	//public final boolean debugMode=true;
	
	private Symbol symb(int type){
		return new Symbol(type, yyline, yycolumn);
	}
	
	private Symbol symb(int type, Object value){
		return new Symbol(type, yyline,yycolumn,value);
	}  
   
%}

nl					=		\n|\r|\r\n

begin_id			=		BEGIN

version_id			=		VERSION

version				=		2"."0

prod_id				=		PRODID

end_id				=		END
		
vcal_id				=		VCALENDAR

vevent_id			=		VEVENT

vtodo_id			=		VTODO

vjournal_id			=		VJOURNAL

valarm_id			= 		VALARM

date		        =   	[12][0-9][0-9][0-9][01][0-9][0-3][0-9]

date_time			=		{date} T [0-2][0-9][0-5][0-9][0-5][0-9] Z?

tzid                =    	TZID

time_zone			=		[a-zA-Z0-9"/""+""-""_"]+

dtstamp_id			=	    DTSTAMP

dtstart_id			=	    DTSTART

dtend_id			=	    DTEND	

summary_id			=	    SUMMARY

organizer_id		=		ORGANIZER

due_id				=		DUE

status_id			=		STATUS

category_id			=		CATEGORIES

description_id		=		DESCRIPTION

duration_id			=		DURATION

dur_value  			= 		("+"|"-")? "P" ({dur_date} | {dur_time} | {dur_week})
dur_date   			= 		{dur_day} ({dur_time})?
dur_time   			= 		"T" ({dur_hour} | {dur_minute} | {dur_second})
dur_week   			= 		[0-9]+ "W"
dur_hour   			= 		[0-9]+ "H" ({dur_minute})?
dur_minute 			= 		[0-9]+ "M" ({dur_second})?
dur_second 			= 		[0-9]+ "S"
dur_day    			= 		[0-9]+ "D"

text	            =       [^":""."";""="][a-zA-Z@"-""/"",""."\\\'\""?""=" 0-9&#%"*"]+

unsupported_prop	=		[A-Z"-"]+

separatore	 	    =  		(":"|";"|"=")

valarm_sintax_start        =      {begin_id}{separatore}{valarm_id}

valarm_sintax_end        =       {end_id}{separatore}{valarm_id} 

%%


{begin_id}    		{if(debugMode) System.out.println("Trovato begin_id: "+yytext());  return symb(ParserSym.begin_id); }

{separatore}   		{if(debugMode) System.out.println("Trovato separatore: "+yytext());  return symb(ParserSym.separatore); }

{version_id}    	{if(debugMode) System.out.println("Trovato version_id: "+yytext()); return symb(ParserSym.version_id); }

{version}$    		{if(debugMode) System.out.println("Trovato versione: "+yytext()); return symb(ParserSym.version); }

{prod_id}    		{if(debugMode) System.out.println("Trovato prod_id: "+yytext());  return symb(ParserSym.prod_id);}

{end_id}   			{if(debugMode) System.out.println("Trovato end_id: "+yytext()); return symb(ParserSym.end_id); }

{vcal_id}$    		{if(debugMode) System.out.println("Trovato vcal_id: "+yytext());  return symb(ParserSym.vcal_id); }

{vevent_id}$    	{if(debugMode) System.out.println("Trovato vevent_id: "+yytext());  return symb(ParserSym.vevent_id); }

{vtodo_id}$			{if(debugMode) System.out.println("Trovato vtodo_id: "+yytext()); return symb(ParserSym.vtodo_id); }

{vjournal_id}$		{if(debugMode) System.out.println("Trovato vjournal_id: "+yytext());  return symb(ParserSym.vjournal_id); }

{duration_id}    	{if(debugMode) System.out.println("Trovato duration_id: "+yytext());  return symb(ParserSym.duration_id); }

{dur_value}$    	{if(debugMode) System.out.println("Trovato dur_value: "+yytext()); return symb(ParserSym.duration_value, new String(yytext())); }

/* Entro in uno stato esclusivo per eliminare le sintassi dei valarm che  non mi interessano  */
  
{valarm_sintax_start}          	{ yybegin(VAL); x++;} 
<VAL>(.|{nl})					{;}
<VAL>{valarm_sintax_start}     	{x++;}
<VAL>{valarm_sintax_end}		{x--; if(x==0) yybegin(YYINITIAL); } 

{dtstart_id}    	{if(debugMode) System.out.println("Trovato dtstart_id: "+yytext());  return symb(ParserSym.dtstart_id);  }

{dtstamp_id}    	{if(debugMode) System.out.println("Trovato dtstamp_id: "+yytext()); return symb(ParserSym.dtstamp_id); }

{date}$    			{if(debugMode) System.out.println("Trovato date: "+yytext()); return symb(ParserSym.date, new String(yytext())); }

{date_time}$		{if(debugMode) System.out.println("Trovato date_time: "+yytext()); return symb(ParserSym.date_time, new String(yytext())); }

{tzid}    			{if(debugMode) System.out.println("Trovato time zone id: "+yytext());  return symb(ParserSym.tzid); }

{dtend_id}    		{if(debugMode) System.out.println("Trovato dtend_id: "+yytext());  return symb(ParserSym.dtend_id); }

{organizer_id}		{if(debugMode) System.out.println("Trovato organizer_id: "+yytext());   return symb(ParserSym.organizer_id, new String(yytext())); }

{due_id}			{if(debugMode) System.out.println("Trovato due_id: "+yytext());  return symb(ParserSym.due_id); }

{summary_id}    	{if(debugMode) System.out.println("Trovato summary_id: "+yytext()); return symb(ParserSym.summary_id); }

{status_id}			{if(debugMode) System.out.println("Trovato status_id: "+yytext());  return symb(ParserSym.status_id); }

{category_id}		{if(debugMode) System.out.println("Trovato category_id: "+yytext());  return symb(ParserSym.category_id); }

{description_id}	{if(debugMode) System.out.println("Trovato description_id: "+yytext());  return symb(ParserSym.description_id); }



{text}$			{if(debugMode) System.out.println("Trovato text: "+yytext());  return symb(ParserSym.text, new String(yytext()
																															.replaceAll("%3A",":")
																															.replaceAll("%3B",";")
																															.replaceAll("%3D","=")  
																															)); }	

^{unsupported_prop} {if(debugMode) System.out.println("Trovato uns. prop: "+yytext());
						yybegin(UNSUPP);  }
						
<UNSUPP>(.*)			{if(debugMode) System.out.println("Trovato carattere generico(UNSUPP): "+yytext()); }

<UNSUPP>{nl}			{yybegin(YYINITIAL); }

{time_zone}			{if(debugMode) System.out.println("Trovato time_zone: "+yytext()); return symb(ParserSym.time_zone, new String(yytext())); }

{nl}|" " 			{;}

.					{if(debugMode) System.out.println("Trovato carattere generico: "+yytext()); }
